﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using DAL;
using Entity;

namespace BLL
{
    public class ServiceBO
    {
        /// <summary>
        /// 创建服务项插入到数据库;
        /// </summary>
        /// <param name="service"></param>
        /// <returns>返回影响的行数</returns>
        public static int InsertService(AppointmentService service)
        {
            return ServiceDAO.InsertService(service);
        }

        /// <summary>
        /// 修改服务项; 返回受影响行数;
        /// </summary>       
        public static int UpdateService(AppointmentService service)
        {
            return ServiceDAO.UpdateService(service);
        }

        /// <summary>
        /// 将相应的表格转为数组;
        /// </summary>
        /// <param name="dt">表格</param>
        /// <returns>数组</returns>
        private static AppointmentService[] ChangeToServices(DataTable dt)
        {
            AppointmentService[] servicesArray = new AppointmentService[]{};

            if (dt != null && dt.Rows.Count > 0)
            {
                servicesArray = new AppointmentService[dt.Rows.Count];
                int i = 0;

                foreach (DataRow row in dt.Rows)
                {                   
                    AppointmentService service = new AppointmentService();
                    service.ServiceId = int.Parse(row["ServiceId"].ToString());
                    service.ServiceName = row["ServiceName"].ToString();
                    service.TimeBuckets = row["TimeBuckets"].ToString();
                    service.DirectorId = int.Parse(row["DirectorId"].ToString());
                    service.Introduction = row["Introduction"].ToString();
                    service.PerMultiTime = float.Parse(row["PerMultiTime"].ToString());
                    service.MultiNum = int.Parse(row["MultiNum"].ToString());
                    service.IsActive = Boolean.Parse(row["IsActive"].ToString());
                   
                    servicesArray[i++] = service;
                }                
            }
            return servicesArray;
        }

        /// <summary>
        /// 为Student获取Service（全部有效的）;
        /// </summary>
        /// <returns>数组</returns>
        public static AppointmentService[] GetServicesForStudent()
        {
            DataTable dt = ServiceDAO.GetAllActiveService();
            return ChangeToServices(dt);
        }

        /// <summary>
        /// 获取指定Director名下的Services;
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static AppointmentService[] GetServicesForDirector(string userId)
        {
            DataTable dt = ServiceDAO.GetServicesForDirector(userId);
            return ChangeToServices(dt);
        }

        /// <summary>
        /// 通过ServiceId的字符串形式获得service对象;
        /// </summary>
        /// <param name="IdStr"></param>
        /// <returns>AppointmentService</returns>
        public static AppointmentService GetServiceByServiceId(string IdStr)
        {
            int serviceId;
            if (!int.TryParse(IdStr, out serviceId))
            {
                return null;
            }

            AppointmentService service = new AppointmentService();

            DataTable dt = ServiceDAO.GetServiceByServiceId(serviceId);
            if (dt != null && dt.Rows.Count > 0)
            {
                service.ServiceId = int.Parse(dt.Rows[0]["ServiceId"].ToString());
                service.ServiceName = dt.Rows[0]["ServiceName"].ToString();
                service.TimeBuckets = dt.Rows[0]["TimeBuckets"].ToString();
                service.DirectorId = int.Parse(dt.Rows[0]["DirectorId"].ToString());
                service.Introduction = dt.Rows[0]["Introduction"].ToString();
                service.PerMultiTime = float.Parse(dt.Rows[0]["PerMultiTime"].ToString());
                service.MultiNum = int.Parse(dt.Rows[0]["MultiNum"].ToString());
                service.IsActive = Boolean.Parse(dt.Rows[0]["IsActive"].ToString());
            }

            return service;
        }

        /// <summary>
        /// 通过ServiceId获得service对象（包括director）;
        /// </summary>       
        public static AppointmentService GetServiceDetail(int serviceId)
        {            
            AppointmentService service = new AppointmentService();

            DataTable dt = ServiceDAO.GetServiceByServiceId(serviceId);
            if (dt != null && dt.Rows.Count > 0)
            {
                service.ServiceId = int.Parse(dt.Rows[0]["ServiceId"].ToString());
                service.ServiceName = dt.Rows[0]["ServiceName"].ToString();
                service.TimeBuckets = dt.Rows[0]["TimeBuckets"].ToString();
                service.DirectorId = int.Parse(dt.Rows[0]["DirectorId"].ToString());
                service.Introduction = dt.Rows[0]["Introduction"].ToString();
                service.PerMultiTime = float.Parse(dt.Rows[0]["PerMultiTime"].ToString());
                service.MultiNum = int.Parse(dt.Rows[0]["MultiNum"].ToString());
                service.IsActive = Boolean.Parse(dt.Rows[0]["IsActive"].ToString());
            }

            service.Director = UserBO.GetUserById(service.DirectorId);

            return service;
        }
    }
}
